package com.likou.arr;

/**
 * 给定一个整数数组 nums 和一个目标值 target，请你在该数组中找出和为目标值的那 两个 整数，并返回他们的数组下标。
 * 你可以假设每种输入只会对应一个答案。但是，你不能重复利用这个数组中同样的元素。
 * Created by HXT on 2019/7/29.
 */
public class ArrAdd {
    public int[] twoSum(int[] nums, int target) {
        /*int indexArrayMax = 2047;
        int[] indexArrays = new int[indexArrayMax + 1];
        int diff = 0;
        for (int i = 0; i < nums.length; i++) {
            diff = target - nums[i];
            System.out.println((diff & indexArrayMax)+"-"+(indexArrays[diff & indexArrayMax]));
            if (indexArrays[diff & indexArrayMax] != 0) {
                return new int[] { indexArrays[diff & indexArrayMax] - 1, i };
            }
            System.out.println((nums[i] & indexArrayMax)+"-"+(indexArrays[nums[i] & indexArrayMax]));
            indexArrays[nums[i] & indexArrayMax] = i + 1;
        }
        throw new IllegalArgumentException("No two sum value");*/
        for (int i = 0; i < nums.length-1; i++) {
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    int [] result ={i,j};
                    return result;
                }
            }
        }
        return null;
    }
    public static void main(String[] args){
        ArrAdd arrAdd = new ArrAdd();
        int [] nums = {2,7,11,15};
        int [] result = arrAdd.twoSum(nums,9);
        for (int i:result) {
            System.out.println(i);
        }
    }
}
